package com.company.test20;

import javax.swing.*;

/**
 * Created by Administrator on 2016.9.27.
 */
public class Test2026 {

    public static void main(String[] args) {
        int[][] gridding=new int[8][8];

        for (int i=0;i<gridding.length;i++){
            for (int j=0;j<gridding.length;j++){
                gridding[i][j]=0;
            }
        }

        background(gridding,0,0);

        for (int i=0;i<gridding.length;i++){
            for (int j=0;j<gridding.length;j++){
                System.out.print(gridding[i][j]);
            }
            System.out.println();
        }

    }

    public static boolean background(int[][] gridding,int x,int y){
        gridding[x][y]=1;
        if(x==7&&y==7) {
            return true;
        }

        boolean z=false;

        if(isNotSquare(gridding,x+1,y)&&isEmpty(gridding,x+1,y)) {
            z = background(gridding, x + 1, y);
        }
        if(z==false&&isNotSquare(gridding,x,y+1)&&isEmpty(gridding,x,y+1)){
            z=background(gridding,x,y+1);
        }
        if(z==false&&isNotSquare(gridding,x,y-1)&&isEmpty(gridding,x,y-1)){
            z=background(gridding,x,y-1);
        }
        if(z==false&&isNotSquare(gridding,x-1,y)&&isEmpty(gridding,x-1,y)){
            z=background(gridding,x-1,y);
        }

        return z;
    }

    public static boolean isNotSquare(int[][] gridding,int x,int y){

        try{
            if(gridding[x+1][y]==1 &&gridding[x][y+1]==1&&gridding[x+1][y+1]==1){
                return false;
            }
        }catch (Exception e){

        }

        try{
            if(gridding[x-1][y]==1&&gridding[x-1][y+1]==1&&gridding[x][y+1]==1){
                return false;
            }
        }catch (Exception e){

        }

        try{
            if(gridding[x+1][y-1]==1&&gridding[x][y-1]==1&&gridding[x+1][y]==1){
                return false;
            }
        }catch (Exception e){

        }

        try{
            if(gridding[x-1][y]==1&&gridding[x-1][y-1]==1&&gridding[x][y-1]==1){
                return false;
            }
        }catch (Exception e){

        }

        return true;
    }

    public static boolean isEmpty(int[][] gridding,int x,int y){
        if(x<0||x>7){
            return false;
        }

        if(y<0||y>7){
            return false;
        }

        return gridding[x][y]==0;
    }
}
